package org.doubango.FrCall.Screens.chat;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.doubango.FrCall.R;
import org.doubango.FrCall.Screens.BaseScreen;
import org.doubango.FrCall.Screens.ScreenHome;
import org.doubango.FrCall.Utils.DateTimeUtils;
import org.doubango.FrCall.Utils.LogDebugger;
import org.doubango.ngn.media.NgnMediaType;
import org.doubango.ngn.model.NgnHistoryEvent;
import org.doubango.ngn.model.NgnHistoryEvent.StatusType;
import org.doubango.ngn.model.NgnHistorySMSEvent;
import org.doubango.ngn.services.INgnHistoryService;
import org.doubango.ngn.utils.NgnPredicate;
import org.doubango.ngn.utils.NgnStringUtils;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ScreenChatHistory extends BaseScreen {
	private static String TAG = ScreenChatHistory.class.getCanonicalName();

	private final INgnHistoryService mHistorytService;
	private ScreenChatAdapter mAdapter;
	private ListView mLvHistoy;
	
	private String user;
	
	public ScreenChatHistory() {
		super(SCREEN_TYPE.CHAT_HISTORY_T, TAG);
		mHistorytService = getEngine().getHistoryService();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_chat_history);
		
		user = ScreenHome.selFriend.getUserId();
		LogDebugger.debug(TAG, "user: " + user);
		
		mLvHistoy = (ListView) findViewById(R.id.screen_chat_listView);
		mAdapter = new ScreenChatAdapter(this);
		mLvHistoy.setAdapter(mAdapter);
		mLvHistoy.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
		mLvHistoy.setStackFromBottom(true);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	/**
	 * HistoryEventSMSFilter
	 */
	class HistoryEventChatFilter implements NgnPredicate<NgnHistoryEvent> {
		@Override
		public boolean apply(NgnHistoryEvent event) {
			if (event != null && (event.getMediaType() == NgnMediaType.SMS)) {
				return NgnStringUtils.equals(user, event.getRemoteParty(), false);
			}
			return false;
		}
	}

	/**
	 * DateComparator
	 */
	class DateComparator implements Comparator<NgnHistoryEvent> {
		@Override
		public int compare(NgnHistoryEvent e1, NgnHistoryEvent e2) {
			return (int) (e1.getStartTime() - e2.getStartTime());
		}
	}
	
	/**
	 * ScreenChatAdapter
	 */
	class ScreenChatAdapter extends BaseAdapter {
		private List<NgnHistoryEvent> mChatList;
		private final LayoutInflater mInflater;
		private final ScreenChatHistory mBaseScreen;

		ScreenChatAdapter(ScreenChatHistory baseSceen) {
			mBaseScreen = baseSceen;
			mInflater = LayoutInflater.from(mBaseScreen);
			mChatList = mBaseScreen.mHistorytService.getObservableEvents().filter(new HistoryEventChatFilter());
			Collections.sort(mChatList, new DateComparator());
		}


		@Override
		public int getCount() {
			return mChatList.size();
		}

		@Override
		public Object getItem(int position) {
			return mChatList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			return false;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.screen_chat_item, null, false);
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.text = (TextView) convertView.findViewById(R.id.text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final NgnHistoryEvent event = (NgnHistoryEvent) getItem(position);

			final NgnHistorySMSEvent SMSEvent = (NgnHistorySMSEvent) event;
			final String content = SMSEvent.getContent();
			final boolean bIncoming = SMSEvent.getStatus() == StatusType.Incoming;
			
			int color;
			String name;
			if (bIncoming) {
				color = 0xFF005aa7;
				name = user.substring(0, user.indexOf("@"));
			} else {
				color = 0xFF63a003;
				name = "我";
			}
			holder.name.setText(name);
			holder.name.setTextColor(color);
			holder.time.setText(DateTimeUtils.getFriendlyDateString(new Date(event.getStartTime())));
//			holder.text.setText(parser.addSmileySpans(content));
			holder.text.setText(content);
			return convertView;
		}
		
		private class ViewHolder {
			TextView name;
			TextView time;
			TextView text;
		}
	}
}
